package com.czk.ioc.benpostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * BeanFactoryPostProcessor获取BeanDefinition
 * BeanFactoryPostProcessor是在spring容器加载了bean的定义文件之后，在bean实例化之前执行的。接口方法的入参是ConfigurrableListableBeanFactory，
 * 使用该参数，可以获取到相关bean的定义信息
 *
 * @Author:ChenZhangKun
 * @Date: 2021/7/23 10:05
 */
public class MyBeanFactoryProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("调用MyBeanFactoryProcessor啦");
        // 拿到beanDefinition
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("student");
        // 拿到属性值
        MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
        //beanDefinition.setBeanClassName("student类");
        System.out.println(beanDefinition.getBeanClassName());
        // 拿到属性值
        if (propertyValues.contains("name")) {
            propertyValues.addPropertyValue("sex", "nv");
        }
        beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    }
}
